package org.bob.learn.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.bob.learn.common.util.RedisUtils;
import org.bob.learn.web.model.Result;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.function.Function;
import java.util.function.Predicate;

@Slf4j
@RestController
@RequestMapping("/error")
public class ErrorController {

    @GetMapping("test")
    Mono<Result> query(@Param(value = "id") Long id){
        Flux.just(1, 2, 3)
                .flatMap(integer -> Flux.range(integer, integer * 2))
                .subscribe(System.out::println);

        return Mono.just(id).map(new Function<Long, Result>() {
            @Override
            public Result apply(Long aLong) {
                if(aLong==0){
                    throw new ArithmeticException("除数为零");
                }
                return Result.Success(100/aLong) ;
            }
        }).onErrorMap(new Function<Throwable, Throwable>() {
            @Override
            public Throwable apply(Throwable throwable) {
                if(throwable instanceof ArithmeticException){
                    log.info("sdfasdf");
                    return new RuntimeException();
                }
                return null;
            }
        }).onErrorReturn(Result.Failure("运算异常"));
    }
}
